/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.impl;

import dao.ChiTietDonDatHangDAO;
import java.util.ArrayList;
import java.util.List;
import pojo.ChiTietDonDatHang;
import pojo.SanPham;
import service.ChiTietDonDatHangService;

/**
 *
 * @author MongKieu
 */
public class ChiTietDonDatHangServiceImpl implements ChiTietDonDatHangService {

    private ChiTietDonDatHangDAO chiTietDonDatHangDAO;

    public ChiTietDonDatHangDAO getChiTietDonDatHangDAO() {
        return chiTietDonDatHangDAO;
    }

    public void setChiTietDonDatHangDAO(ChiTietDonDatHangDAO chiTietDonDatHangDAO) {
        this.chiTietDonDatHangDAO = chiTietDonDatHangDAO;
    }

    @Override
    public List<ChiTietDonDatHang> listChiTietDonDatHang() {
        return chiTietDonDatHangDAO.listChiTietDonDatHang();
    }

    @Override
    public List<ChiTietDonDatHang> listChiTietDonDatHangBanChayNhat(int soLuongSanPhamMax) {
        return chiTietDonDatHangDAO.listChiTietDonHangBanChayNhat(soLuongSanPhamMax);
    }

    @Override
    public int soLuongChiTietDonHangBanChayNhat(int soLuongSanPHamMax) {
        return chiTietDonDatHangDAO.listChiTietDonHangBanChayNhat(soLuongSanPHamMax).size();
    }

    @Override
    public List<ChiTietDonDatHang> listChiTietDonDatHangBanChayNhatCoPhanTrang(int trang) {
        int soSanPhamTrenTrang = 5;
        int batDau = (trang - 1) * soSanPhamTrenTrang;
        return chiTietDonDatHangDAO.listChiTietDonDatHangBanChayNhatCoPhanTrang(batDau, soSanPhamTrenTrang);
    }

    @Override
    public int tinhSoLuongTrang(int soLuongSanPhamMax) {
        int soTrang = 0;
        int soLuongSanPhamTrenTrang = 5;
        int soLuong = chiTietDonDatHangDAO.listChiTietDonHangBanChayNhat(soLuongSanPhamMax).size();
        soTrang = soLuong / soLuongSanPhamTrenTrang;
        if (soLuong % soLuongSanPhamTrenTrang != 0) {
            soTrang++;
        }
        return soTrang;
    }

    @Override
    public List<SanPham> listSanPhamBanChayNhat(int soLuongSanPhamMax) {
        List<SanPham> list = new ArrayList<SanPham>();
        List<ChiTietDonDatHang> listChiTietDonDatHang = chiTietDonDatHangDAO.listChiTietDonHangBanChayNhat(soLuongSanPhamMax);
        for (int i = 0; i < listChiTietDonDatHang.size(); i++) {
            ChiTietDonDatHang ct = listChiTietDonDatHang.get(i);
            list.add(ct.getSanPham());
        }
        return list;
    }
}
